package com.gui.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gui.cmn.client.DictFeignClient;
import com.gui.common.result.Result;
import com.gui.hosp.repository.HospitalRepository;
import com.gui.hosp.service.HospitalService;
import com.gui.model.hosp.Hospital;
import com.gui.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author GodLamp
 * @date 2022/10/18 13:10
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private HospitalRepository hospitalRepository;

    @Resource
    private DictFeignClient dictFeignClient;

    /**
     * 上传医院信息
     * @param params
     */
    @Override
    public void save(Map<String, Object> params) {
        //把参数map转换成 Hospital对象
        String mapString = JSONObject.toJSONString(params);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        //判断是否存在该对象
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());

        if (hospitalExist != null) {
            //如果存在，修改
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);

            hospitalRepository.save(hospital);

        } else {
            //如果不存在，则添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);

            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //注意当前页时从0开始
        Pageable pageable = PageRequest.of(page - 1, limit);

        ExampleMatcher exampleMatcher = ExampleMatcher
                .matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        Example<Hospital> example = Example.of(hospital ,exampleMatcher);

        //查询出的所有医院（不包含医院的等级信息）
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        //遍历医院，封装医院等级、省市县信息
        pages.getContent().stream().forEach(hosp -> {
            this.setHospitalHosType(hosp);
        });

        return pages;
    }

    /**
     * 更新医院信息
     * @param id
     * @param status
     */
    @Override
    public void update(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();

        if (hospital != null) {
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Map<String, Object> getHospbyId(String id) {
        Map<String, Object> map = new HashMap<>();

        Hospital hospital = setHospitalHosType(hospitalRepository.findById(id).get());

        //医院基本信息（包含等级信息）
        map.put("hospital", hospital);
        //医院预约规则信息
        map.put("bookingRule", hospital.getBookingRule());

        //不需要返回重复信息
        hospital.setBookingRule(null);

        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital == null) {
            return null;
        }

        return hospital.getHosname();
    }

    /**
     * 封装医院等级,省市地区信息
     * @param h
     */
    private Hospital setHospitalHosType(Hospital h) {
        //查询医院等级名称
        String hostype = dictFeignClient.getName("Hostype", h.getHostype());

        //查询省，市，地区
        String provinceName = dictFeignClient.getName(h.getProvinceCode());
        String cityName = dictFeignClient.getName(h.getCityCode());
        String distrName = dictFeignClient.getName(h.getDistrictCode());

        h.getParam().put("hostypeString", hostype);
        h.getParam().put("fullAddress", provinceName + cityName + distrName);

        return h;
    }

}
